<?php
require_once(dirname(__FILE__)."/../../libs/dbclass/dbconnect.php");
require_once(dirname(__FILE__)."/../../libs/string/libstr.php");
$mc= new libstr();

class article{
	
//删除商品类别
function cata_del($rootid){
	global $mc,$db;
	if($mc->Ismp($rootid)!=""){
$myrs4=$db->dvstr("select `lock` from article_class where rootid ='".$rootid."' ");

	if(trim($myrs4["lock"])=="1")
	{
		//return "cata is lock";限制删除
	}
	
		$db->cmd("delete from article_class where rootid like '%".$rootid."%'");//连子集一起删除
		return true;
	}
	return false;
}
	//添加商品类别
	function cata_add($classname,$pid=0,$content="",$order=0){
		global $mc,$db;
		if( $mc->Ismp($classname)==""){
			return false;
		}
	$rootid="0,";
	if($pid!=0){
	$rs1=$db->ds("select * from article_class where ID=".trim($pid)." order by ID asc ");
	if(count($rs1)>0){
		$rootid=trim($rs1[0]["rootid"]);
	}
	}
	$sqlstr="insert into article_class(classname,classorder)values('".trim($classname)."','".$order."')";
	$db->cmd($sqlstr);
	$newID = $db->insert_id();
    $rootid.=$newID.",";
	$depthid = 0;
	$mydepthid = split(",",$rootid);
	if (count($mydepthid) >= 0)
	{
		$depthid = count($mydepthid) - 3;
	}
$db->cmd("update article_class set rootid='".$rootid."',pid=".$pid.",content='".$content."',depthid=".$depthid."  where ID=".$newID."");
return true;	
	}
	//判断是否有子集
function myck($id)
{

	global $db;
	$rs1=$db->ds("select * from article_class where pid='".trim($id)."'");
	if(count($rs1)>0){
		return true;
	}else{
		return false;
	}
}

//修改类别
function cata_edit($id,$classname,$pid=0,$content="",$order=0){
	global $mc,$db;

		if(  $mc->Ismp($id)=="" || $mc->Ismp($classname)==""){
			return false;
		}
		if($id==$pid){
		$db->cmd("update article_class set content='".$content."',classname='".$classname."',classorder='".$classorder."' where ID=".trim($id)."");
		return true;	
		}else{
		if($pid!=0){
	$rs1=$db->ds("select * from article_class where ID=".trim($pid)." order by ID asc ");
	if(count($rs1)>0){
		$rootid=trim($rs1[0]["rootid"]);
		$rootid.=$id.",";
	}
	}else{
		$rootid="0,".$id.",";
	}	
	$depthid = 0;
	$mydepthid = split(",",$rootid);
	if (count($mydepthid) >= 0)
	{
		$depthid = count($mydepthid) - 3;
	}
	if($this->myck($id)=='true'){
	$db->cmd("update article_class set content='".$content."',classname='".$classname."',classorder='".$classorder."' where ID=".trim($id)."");
		return true;	
	}
		//return "update product_class set content='".$content."' ,classname='".$classname."',classorder='".$order."',pid=".$pid." ,rootid='".$rootid."',depthid=".$depthid."  where ID=".$id."";
	$db->cmd("update article_class set content='".$content."' ,classname='".$classname."',classorder='".$order."',pid=".$pid." ,rootid='".$rootid."',depthid=".$depthid."  where ID=".$id."");
	return true;
		}
		
	
}
//获得类别详细
function cata_detail($id){
	global $mc,$db;
	if(  $mc->Ismp($id)==""){
			return false;
		}
	$rs1=$db->dvstr("select * from article_class where ID=".trim($id)." order by ID asc limit 1 ");
	return 	$rs1;

}
//生成文章编号
function setcodeid(){
		$bianhao=date("YmdHis").rand(1000,9999);
		return $bianhao;
}
//转化级别类名
function getclassname($rootid){
	global $mc,$db;
	$cc=$db->dvstr("select * from article_class where rootid='".$rootid."' limit 1");
	return $cc["classname"];
}	

/** 
 * product_add
 * 
 * 添加文章
 * @param 字符类型 $topicname 产品标题
 * @param 数字类型 $codeid 产品编号
 * @param 数字类型 $nianji 年级属性 单选 
 * @param 数字类型 $xueke 学科属性 多选  
 * @return  布尔值 or string
 *  */
function article_add($topicname,$codeid,$author,$cataid,$tuijie,$content,$biaoqian,$keyword,$pic,$durl,$daxiao,$adduserno,$isshow=1){
	global $mc,$db;
	$pd=array();
	$dt=array();
	if($mc->Ismp($topicname)==""){
		return "topicname is null";
	}
	$pd["topicname"]=$topicname;
	$pd["author"]=$author;
	$pd["codeid"]=$codeid;
	$rootid=$db->myrootid("article_class",$cataid);
	$pd["rootid"]=$rootid;
	$pd["tuijie"]=$tuijie;
	$pd["jianjie"]=$mc->cut_str($content,200);
	$pd["biaoqian"]=$biaoqian;
	$pd["keyword"]=$keyword;
	$pd["pic"]=$pic;
	$pd["durl"]=$durl;
	$pd["daxiao"]=$daxiao;
	$pd["isshow"]=$isshow;
	$pd["adduserno"]=$adduserno;
	$pd["createtime"]=$_SERVER["REQUEST_TIME"];
	$db->add_data("article_content",$pd);//保存到产品表
	$productid=$db->insert_id();
	$dt["articleid"]=$productid;
	$dt["content"]=$content;
	$db->add_data("article_detail",$dt);//保存到产品详细表
	
	return true;
	
	
}
/** 
 * product_edit
 * 
 *  */
function article_edit($articleid,$topicname,$author,$cataid,$tuijie,$content,$biaoqian,$keyword,$pic,$durl,$daxiao){
	global $mc,$db;

	$pd=array();
	$dt=array();
	if(!$mc->isnum($articleid)){
		return "articleid is not num";
	}
	if($mc->Ismp($topicname)==""){
		return "topicname is null";
	}
	$pd["topicname"]=$topicname;
	$pd["author"]=$author;
	$rootid=$db->myrootid("article_class",$cataid);
	$pd["rootid"]=$rootid;
	$pd["tuijie"]=$tuijie;
	$pd["jianjie"]=$mc->cut_str($content,200);
	$pd["biaoqian"]=$biaoqian;
	$pd["keyword"]=$keyword;
	$pd["pic"]=$pic;
	$pd["durl"]=$durl;
	$pd["daxiao"]=$daxiao;
	$pd["updatetime"]=$_SERVER["REQUEST_TIME"];
$db->update_data("article_content",$pd," id=".$articleid);
	$db->cmd("update article_detail set content='".$content."' where articleid=".$articleid);//保存到产品详细表
	
	return true;
	
}

//获得文章列表
	function articlelist($string="*",$cataid="",$topicname="",$codeid="",$isshow="",$order="",$sj="desc",$pagesize=10,$page=1)
	{
		global $mc,$db;

		$_string="*";
		if($string!="")
		{
			$_string=$string;
		}		
		$_rootid="";
		if($cataid!="")
		{ 
			if($mc->isnum($cataid)=="true"){
				$rootid=$db->myrootid("article_class",$cataid);
			$_rootid=" and rootid like '%".trim($rootid)."%' ";
			}
			
		}	
		$_topicname="";
		if($topicname!="")
		{
			$_topicname=" and topicname like '%".trim($topicname)."%' ";
		}
		$_codeid="";
		if($codeid!="")
		{
			$_codeid=" and codeid=".$codeid." ";
		}
	
		$_isshow="isshow>=0";	
		if($isshow!="")
		{				
			$_isshow=" isshow=".$isshow." ";
		}
		
	
		$_where=$_isshow.$_rootid.$_topicname.$_codeid;
		
		$_tb="article_content";
		$_order="";
		if($order!="")
		{
			$_order=" order by ".$order." ".$sj." ";	
		}
				
		$_pagesize=$pagesize;
		$_page=$page;
					
		$totalcount_xiaoquan=$db->getcount("id",$_tb,$_where);		
		$getrs=$db->pagerds($_string,$_tb,$_where,$_order,$totalcount_xiaoquan,$_pagesize,$_page);
		$i=0;
		if($totalcount_xiaoquan>0){
		foreach($getrs as $v)
		{
			$getrs[$i]=$v;
			$getrs[$i]["totalcount"]=$totalcount_xiaoquan;
			$i=$i+1;
		}
		}
		return $getrs;
		
	}
//获得资源详细
function getarticleshow($id){
	global $mc,$db;
	$mbshow=array();
	if($mc->Ismp($id)!=""){
	$mbshow=$db->ds("select * from article_content where id=".$id." limit 1");	
	$dt=$db->ds("select content from article_detail where articleid=".$id." limit 1");
	$mbshow[0]["content"]=$dt[0]["content"];
	}
	return $mbshow[0];
}

//删除多个资源
function article_del($arr){
	global $mc,$db;
	$str=sprintf("%s",implode(",",$arr));
	$db->cmd("delete from article_content where id in(".$str.")");
	$db->cmd("delete from article_detail where articleid in(".$str.")");
	return true;	
}

//删除一个资源
function article_delone($id){
	global $mc,$db;
	
	$db->cmd("delete from article_content where id =".$id."");
	$db->cmd("delete from article_detail where articleid=".$id."");
	return true;	
}

//更新多个产品状态
function updateisshow($arr,$isshow=1){
	global $db;
	$str=sprintf("%s",implode(",",$arr));
	$db->cmd("update article_content set isshow=".$isshow."  where id in(".$str.")");
	return true;
}
//更新1个产品状态
function updateoneisshow($id){
	global $db;

	$db->cmd("update article_content set isshow=".$isshow."  where id=".$id);
	return true;
}

//产品状态
function showzhuangtai($str)
{
	if($str=="-1")
	{
		return "不通过";
	}
	if($str=="0")
	{
		return "未审核";
	}
	if($str=="1")
	{
		return "已审核";
	}
	if($str=="-2")
	{
		return "回收站";
	}
	return '--';
}
	
}
?>